﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Threading;
using System.Timers;
using System.IO;
using SMALLERP.ComClass;

namespace XJLSC.QM
{
    
    public partial class PZMSHKB : Form
    {
        
        Thread myThread;
        public int frequency = 10000;//更新时间频率
        public static bool isUse = false;//是否停止更新
        public static string statusInfo = string.Empty;//状态
        private delegate void myDelegate(DataTable dt);//定义委托
        private readonly DataBase db = new DataBase();
        public PZMSHKB()
        {
            InitializeComponent();
            label3.Text = "更新频率为:" + (frequency / 1000).ToString() + "秒";
        }
        delegate void show();
        Thread t = null;
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {

                Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                    
             
        }
       
        private void PZMSHKB_Load(object sender, EventArgs e)
        {
            trackBar1.Value = 300000;
            radioButton2.Checked = true;
            //开始查询数据
            myThread = new Thread(startFillDv);//实例化线程
            myThread.Start();
            t = new Thread(new ThreadStart(ShowLbl));
            t.Start();
               
        }

          public void ShowLbl()
        {
            while (true)
            {
                if (panel1.InvokeRequired)
                {
                    panel1.Invoke(new show(GetConfig));
                }
                Thread.Sleep(200);
            }
        }

        private void GetConfig()
        {
            try
            {  string kanban="";

            if (radioButton1.Checked)
            {
                kanban = OperatorFile.GetIniFileString("KanBan", "kanban2", "",
                                                     Application.StartupPath + "\\QM.ini");
            }

            if (radioButton2.Checked)
            {
                kanban = OperatorFile.GetIniFileString("KanBan", "kanban2", "",
                                                     Application.StartupPath + "\\QM.ini");
            }

            if (radioButton3.Checked)
            {
                kanban = OperatorFile.GetIniFileString("KanBan", "kanban3", "",
                     
                                Application.StartupPath + "\\QM.ini");
            }


            if (radioButton4.Checked)
            {
                kanban = OperatorFile.GetIniFileString("KanBan", "kanban4", "",

                                Application.StartupPath + "\\QM.ini");
            }

                int k = System.Text.Encoding.Default.GetByteCount(kanban);
                if (k < 128)
                {
                    for (int i = k; i <= 128; i++)
                    {
                        kanban += " ";
                    }
                }
                this.label2.Text = kanban;
                this.label1.Text = kanban;                                                                                                               
                this.label1.Left = label1.Left - 10;
                this.label2.Left = label1.Left + this.label1.Size.Width;
                if (label1.Left + label1.Size.Width <= 0) 
                {
                    label1.Left = label2.Left;
                    label2.Left = label1.Left + this.label1.Size.Width;
                }
            }
            catch (Exception ex)
            {
            }
        }


        private void startFillDv()
        {
            while (true)
            {
                if (isUse)
                {
                    statusInfo = "正在实时更新数据......";
                    string strSql = null;
                    if (radioButton1.Checked)
                    {
                        strSql = "SELECT CD001+'-'+CD002+'-'+CD003 送检单号,MA002 供应商,INVMB.UDF01 老编码,CD004 品号,CD005 品名,CD006 规格,CD008 待送检数量,CD007 单位,PURCD.CREATE_DATE 送货日期 FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN PURMA ON CC005=MA001 LEFT JOIN INVMB ON MB001=CD004 WHERE CD034='N' ORDER BY PURCD.CREATE_DATE DESC";
                    }

                    if (radioButton2.Checked)
                    {
                        strSql = "SELECT CD001+'-'+CD002+'-'+CD003 送检单号,MA002 供应商,INVMB.UDF01 老编码,CD004 品号,CD005 品名,CD006 规格,CD008 送检数量,CD007 单位,PURCD.CREATE_DATE 送检日期 FROM PURCD LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN PURMA ON CC005=MA001 LEFT JOIN INVMB ON MB001=CD004 WHERE NOT EXISTS(SELECT * FROM QMSTB WHERE TB001=CD001 AND TB002=CD002 AND TB003=CD003) ORDER BY PURCD.CREATE_DATE DESC";
                    }

                    if (radioButton3.Checked)
                    {
                        strSql = "SELECT TY001+'-'+TY002 送检单号,MD002 工作中心,INVMB.UDF01 老编码,TA006 品号,MB002 品名,MB003 规格,TY004 送检数量,TA007 单位,(CASE WHEN ISNULL(MOCTY.UDF07,'')='' THEN '待检' ELSE RTRIM(MOCTY.UDF07) END) AS 检验状态 FROM MOCTY LEFT JOIN MOCTA ON TY001=TA001 AND TY002=TA002 LEFT JOIN INVMB ON MB001=TA006 LEFT JOIN CMSMD ON TA021=MD001 WHERE TY003=CONVERT(CHAR(8),GETDATE(),112) AND TY019='Z04'";
                    }

                    if (radioButton4.Checked)
                    {
                        strSql = "SELECT TO001+'-'+TO002+'-'+TO003 送检单号,TN004 客户编号,TO007 品号,MB002 品名,MB003 规格,TO024 送检数量,TO010 单位,(CASE WHEN ISNULL(COPTO.UDF07,'')='' THEN '待检' ELSE RTRIM(COPTO.UDF07) END) 检验状态 FROM COPTO LEFT JOIN COPTN ON TO001=TN001 AND TO002=TN002 LEFT JOIN INVMB ON TO007=MB001 WHERE TN003=CONVERT(CHAR(8),GETDATE(),112)";
                    }

                

                    try
                    {
                        DataTable dt;
                       
                        //dataGridView1.DataSource = "";
                        dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                        Grid(dt);
                        //dataGridView1.DataSource = dt;
                        //dataGridView1.Refresh();
                        //this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                       // this.dataGridView1.AllowUserToAddRows = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                    Thread.Sleep(frequency);
                }
                else
                {
                    statusInfo = "停止更新!";
                }
            }
        }

        private void Grid(DataTable dt)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDelegate(Grid), new object[] { dt });
            }
            else
            {
                try
                {
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = dt;
                    dt = null;
                    statusInfo = "更新完成!";
                }
                catch
                {

                }
            }

        }

        private void PZMSHKB_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.myThread.IsAlive)
            {
                this.myThread.Abort();//结束线程
            }
           
        }


        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                isUse = true;
            }
            else
            {
                isUse = false;
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label3.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = statusInfo;
            frequency = trackBar1.Value;
          
            //if (statusInfo.Trim() == "正在实时更新数据......")
            //{
            //    pictureBox1.Visible = true;
            //}
            //else
            //{
            //    pictureBox1.Visible = false;
            //}
        }

        private void PZMSHKB_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null)
            {
                t.Abort();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }

       




 
    }
}
